home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6118 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: news.microsoft.com!news
  2. From: a-cnadc@microsoft.com (Dann Corbit)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What's better & why
  5. Date: 22 Feb 1996 19:31:58 GMT
  6. Organization: Microsoft Corporation
  7. Message-ID: <4gigbe$t4m@news.microsoft.com>
  8. References: <31297C5A.E6C@connix.com> <4gf4s6$fl0@kannews.ca.newbridge.com>
  9. NNTP-Posting-Host: 157.57.171.202
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.93.14
  12.  
  13. In article <4gf4s6$fl0@kannews.ca.newbridge.com>, gminer@Newbridge.COM says...
  14. >
  15. >>I was just curious what you all though about the following code.
  16. >>Please tell me what is better (faster/Smaller) and why. Or does it make 
  17. >>any difference at all. I just though about this while I was programming 
  18. >>today?
  19. >>
  20. >>example 1:
  21. >>                val = 1;
  22. >>                if( what ever...)val = 0;
  23. >>
  24. >>or
  25. >>example 2:
  26. >>                if( what ever... )val = 0;
  27. >>                else val = 1;
  28. >
  29. >What an interesting question :) I'm going to go home, code this and then 
  30. >decompile it into ASM to see if it does it this way (my compiler may be 
  31. >smart, though).
  32. >
  33. >If the comiler was dumb, you would get something like the following ASM 
  34. >instructions:
  35. >
  36. >example 1:
  37. >                mov val, 1
  38. >                cmp what, ever
  39. >                jne End
  40. >                mov val, 0
  41. >        End:
  42. >
  43. >2 memory moves, 1 compare, one conditional jump. Fewer instructions = 
  44. >smaller code. 3 or 4 instructions executed.
  45. >
  46. >example 2:
  47. >                cmp what, ever
  48. >                jne End
  49. >                mov val, 0
  50. >                jmp Finish
  51. >        End:    mov val 1
  52. >        Finish:
  53. >
  54. >2 memory moves, 1 compare, one conditional jump, on unconditional jump.
  55. >
  56. >Slightly larger code, one instruction, wee :) Again, 3 or 4 instrucitons 
  57. >executed.
  58. >
  59. >But: with the worst case in example 1, you are executing an extra move, 
  60. >and with the worst case in example 2, you are executing an extra 
  61. >unconditional jump. Which is faster? I don't have my ASM handbook here so 
  62. >I couldn't tell you ;) If I had to guess, I'd say the jmp would be faster 
  63. >becuase your val data wouldn't be registers and all.
  64. >
  65. >So, if I wanted speed, like I usually do, and I was willing to pay the 
  66. >extra few bytes in code size, I'd stay with example 2.
  67. >
  68. >Now, I'm no asm genious, and I don't know how smart my compiler is. It 
  69. >may well realize what you are doing and do things an even better way.
  70. >
  71. >I'm still tempted to go home and code it in c, and see what the asm turns 
  72. >out to be :)_
  73. >
  74. >Thanx for the interesting question :)
  75. Your answer depends upon the particular architecture, compiler, and optimizer
  76. setting for one system.  It may be that something that's faster on one system
  77. is slower on another.  It also can occur that saving one or two cycles can
  78. disable a global optimization that would save much more.  Back to the original
  79. example:
  80. >>example 1:
  81. >>                val = 1;
  82. >>                if( what ever...)val = 0;
  83. >>
  84. >>or
  85. >>example 2:
  86. >>                if( what ever... )val = 0;
  87. >>                else val = 1;
  88. Example 1 sometimes does two assignments.  Example 2 only does one.  If
  89. "what ever..." is usually true, then two assignments are usually done.
  90. ( for example 1, that is.)
  91.  
  92. 90% of the time, it is better to make code clear and understandable than
  93. to tweak out a couple extra cycles.  Using a profiler, identify the hot
  94. spots in your code that need speed, and tweak them.  Often, cranking
  95. up the optimization level does a pretty good job of optimizing for us.
  96.  
  97. 80% of software cost is maintenance.
  98. -- 
  99. The opinions expressed in this message are my own personal views 
  100. and do not reflect the official views of Microsoft Corporation.
  101.  
  102.